<?php
require_once '../global.inc.php';
Database::connect($HOST, $NAME, $PWD, $DB);
// operation操作类型：add、del、query、edit
$operation 		= $_GET["op"];

//查询用户积分
if ($operation=="query") {
	//获取页面信息
	$memberName 	= $_POST["memberName"];
	$netName 		= $_POST["netName"];
	$memberNo		= $_POST["memberNo"];
	$idNumber		= $_POST["idNumber"];
	$mobile			= $_POST["mobile"];
	$scoreBegin		= $_POST["scoreBegin"];
	$scoreEnd		= $_POST["scoreEnd"];
	if(!isset($_GET["p"])){//首次查询
		$condition = " flag=1 ";        //只能调整有效用户
		$tag	   = $_GET["tag"];      //查询后的操作方式（列表，列表编辑）
		if(strlen($memberName)!=0){
			$condition .= " and member_name like '%$memberName%'";
		}
		if(strlen($netName)!=0){
			$condition .= " and net_name like '%$netName%'";
		}
		if(strlen($memberNo)!=0){
			$condition .= " and member_no='$memberNo'";
		}
		if(strlen($idNumber)!=0){
			$condition .= " and id_number='$idNumber'";
		}
		if(strlen($mobile)!=0){
			$condition .= " and mobile=$mobile";
		}
		if(strlen($scoreBegin)!=0){
			$condition .= " and score>=$scoreBegin";
		}
		if(strlen($scoreEnd)!=0){
			$condition .= " and score<=$scoreEnd";
		}
		$_SESSION["memberQryWhere"] = $condition;
		$_SESSION["tag"]			= $tag;
	}else{
		$condition = $_SESSION["memberQryWhere"];
		$tag	   = $_SESSION["tag"];
	}
	$count			= Database::count("member",$condition);
	$totalRows		= $count[0];				//数据总量
	$currentPage	= $_GET["p"];				//当前页面
	$pageInfo		= array();					//初始化页面信息
	$subpages 		= new SubPages($PAGE_SIZE, $totalRows, $currentPage);	//实例化分页
	$pageInfo 		= $subpages->getPageArray();
	
	
	//查询本页数据
	$memberArray 		= Database::findPage("member", $pageInfo["startRow"], $pageInfo["pageSize"],$condition);

	//将分页信息和本页数据写入session
	$_SESSION["pageInfoMember"]	= $pageInfo;
	$_SESSION["memberArray"]	= $memberArray;

	echo "<script>window.location.href='../view/score/scoreList.php';</script>";
}

//获取某一个会员的积分明细
if ($operation=="get") {
	$id				= $_GET["id"];
	$tag			= $_GET["tag"];
	
	//查询日志信息
	$sql			= "select * from log_score where member_id =$id  order by log_id desc";
	$logScoreArray	= Database::find($sql);
	$memberInfo		= Database::findById("member", "member_id", "$id");

	//将取得的会员信息写入session
	$_SESSION["logScoreArray"]		= $logScoreArray;
	$_SESSION["memberInfo"]			= $memberInfo;
	//转向处理页面
	if($tag=="e"){
		echo "<script>window.location.href='../view/score/scoreEdit.php?id=$id';</script>";
	}else{
		echo "<script>window.location.href='../view/score/scoreDetail.php?id=$id';</script>";
	}
}
//修改会员的积分
if ($operation=="edit") {
	$id					= $_GET["id"];
	$totalScore			= $_POST["totalScore"];
	$changeType			= $_POST["changeType"];
	$changeMethod		= $_POST["changeMethod"];
	$changeScore		= $_POST["changeScore"];
	$changeReason		= $_POST["changeReason"];
	$comments			= $_POST["comments"];

	Database::begin();

	if($changeMethod =="1"){//调整方式为增加
		$remainScore = $totalScore+$changeScore;
		$sql = "update member set score = $remainScore where member_id = $id";
	}else{//调整方式为减少
		if($totalScore<$changeScore){//积分少于要减少的积分
			echo "<script>alert('该会员的积分不够用于消费或调整！');</script>";
			echo "<script>window.location.href='scoreAction.php?op=get&id=$id&tag=e';</script>";
		}else{//原来的积分足够用来减少
			$remainScore = $totalScore-$changeScore;
			$sql = "update member set score = $remainScore where member_id = $id";
		}
	}
	Database::update($sql);
	//记录日志
	$userName = $_SESSION["user"]["user_name"];
	$changeDate = date('Y-m-d');
	$sql = "insert into log_score(member_id,change_type,change_method,change_score,remain_score,change_reason,change_date,user_name,comments) "
		  ."values($id,$changeType,$changeMethod,$changeScore,$remainScore,'$changeReason','$changeDate','$userName','$comments')";
	Database::insert($sql);
	Database::commit();
	Database::end();
	echo "<script>alert('调整成功！');</script>";
	echo "<script>window.location.href='scoreAction.php?op=get&id=$id&tag=e';</script>";

}

//关闭数据库
Database::close();
?>